package cc.shacocloud.luanniao.core.model.constant

import cc.shacocloud.luanniao.core.service.text.splitter.PreProcessingRuleService

/**
 * 预处理规则
 * @author 思追(shaco)
 */
enum class PreProcessingRuleTypeEnum : PreProcessingRuleService {

    /**
     * 删除多余空格
     */
    REMOVE_EXTRA_SPACES {

        private val lineBreaksRegex = "\\n{2,}".toRegex()
        private val blanksRegex =
            "[\\t\\f\\r\\u0020\\u00a0\\u1680\\u180e\\u2000-\\u200a\\u202f\\u205f\\u3000]{2,}".toRegex()

        override fun preProcessing(text: String): String {
            var newText = text
            newText = lineBreaksRegex.replace(newText, "\n")
            return blanksRegex.replace(newText, " ")
        }

    },

    /**
     * 删除链接地址，邮箱
     */
    REMOVE_URLS_EMAILS {

        private val emailRegex = "[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+".toRegex()
        private val urlRegex = "https?://\\S+".toRegex()

        override fun preProcessing(text: String): String {
            var newText = text
            newText = emailRegex.replace(newText, "")
            return urlRegex.replace(newText, "")
        }

    }

}

